package com.joysuch.wwyt.risk.repository;

import com.joysuch.wwyt.risk.entity.RiskPointType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface RiskPointTypeDao extends JpaRepository<RiskPointType, Long> {

    int countByNameAndParentId(String name, Long parentId);

    @Query(value = "SELECT * FROM  `wwyt_risk_point_type` WHERE parent_id IS NULL AND delete_flag = '0'", nativeQuery = true)
    List<RiskPointType> findFatherType();

    List<RiskPointType> findByParentId(Long id);

    int countByCode(String code);

    RiskPointType findByCode(String typeCode);

    RiskPointType findByCodeAndParentId(String subType, Long id);

    @Query(value = "SELECT MAX(order_number) FROM  `wwyt_risk_point_type` WHERE parent_id =? AND delete_flag = '0'", nativeQuery = true)
    Integer getMaxOrderNumberByParentId(Long parentId);

    List<RiskPointType> findByName(String pointTypeName);

    RiskPointType findFirstByName(String pointType);

    @Query(value = "SELECT s.* " +
            "FROM wwyt_risk_point_type f " +
            "INNER JOIN wwyt_risk_point_type s " +
            "    ON f.id =s.parent_id " +
            "WHERE s.delete_flag = '0' " +
            "        AND f.delete_flag = '0' " +
            "        AND f.code = ?1 " +
            "        AND s.name = ?2 LIMIT 1",nativeQuery = true)
    RiskPointType findByParentCodeAndName(String type, String subTypeName);
}
